package com.ydx.meituan.utils;

import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class SignUtil {
    public static String SING_KEY="abc123";

    private static final Logger logger = LoggerFactory.getLogger(SignUtil.class);

    public static String sign(Map<String, Object> params, String signKey) throws Exception {

        Map<String, String> needVerify = new HashMap<>();
        Map<String, Object> signMap = Maps.filterEntries(params, entry -> null != entry.getValue() && StringUtils.isNotBlank(String.valueOf(entry.getValue())));
        TreeMap<String, String> sortMap = new TreeMap<>();
        for (Map.Entry<String, Object> entry : signMap.entrySet()) {
            needVerify.put(entry.getKey(), String.valueOf(entry.getValue()));
        }
        //sign_key 是美团分配的密钥
        needVerify.put("sign_key", signKey);
        sortMap.putAll(needVerify);
        List<Map.Entry<String, String>> entryList = new ArrayList<>(sortMap.entrySet());

        StringBuilder signStr = new StringBuilder();
        for (Map.Entry<String, String> entry : entryList) {
            signStr.append(entry.getKey()).append(entry.getValue());
        }
        logger.info("sha1 签名参数:{}", signStr.toString());
        try {
            return org.apache.commons.codec.digest.DigestUtils.sha1Hex(signStr.toString());
        } catch (Exception e) {
            logger.warn("sha1 签名异常,参数:{}", signStr.toString(), e);
            throw e;
        }
    }




    public static String authSign(){
        return null;
    }


}
